# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'raportMonth.ui'
#
# Created: Thu May 31 21:45:14 2012
#      by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
from pl.edu.budgetanalizer.GUI.raportsManagment import CategoryWidget
from pl.edu.budgetanalizer.model.Category import Category
import pyodbc
import matplotlib.pyplot as plt
import numpy



try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s


class Ui_Form(QtGui.QWidget):
    
    '''
    slownik kategorii postaci: {'nazwaKategorii': [podsumaMiesiac1, podsumaMiesiac2, podsumaMiesiac3]}
    '''
    listOfCategories = {}
    
    '''months - lista miesiecy, whole amounts, lista podsum dla danych kategori uporzadkowana zgodnie z lista miesiecy '''
    months = []
    wholeAmount = []
    categories = []


    '''
    lista poszczegolnnych kolum - jest ich tyle ile miesiecy w danym przedziale
    '''
    listOfAmountVerticalLayouts = []
        
    def getAllCategories(self):
        '''
        pobiera wszystkie kategorie i ich podsumy, wsadza do slownika
        '''
        self.wholeAmount = [] 
        self.listOfCategories.clear()
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        cur.execute("select * from Categories")

           
        tmp = self.startdatafield.date()
        finish = self.enddatafield.date()
        
        #tworzenie listy z miesiacami        
        while(tmp <= finish):
            tmpShortDate = str(tmp)
            tmpShortDate = tmpShortDate[tmpShortDate.find("(")+1:tmpShortDate.find(")")]
            tmpShortDate = tmpShortDate.replace(", ", "-")
            tmpShortDate = tmpShortDate[:tmpShortDate.rfind("-")]
            self.months.append(tmpShortDate)
            tmpShortDate = ""
            tmp = tmp.addMonths(1)
        
            
        rows = cur.fetchall()
        for row in rows:
            category = Category(row.name)
            self.categories.append(category.name)
            tmp = self.startdatafield.date()
            self.listOfCategories.fromkeys(category.name)                        
            wholeAmountForCategory = []
            while(tmp <= finish):
                tmpShortDate = str(tmp)
                tmpShortDate = tmpShortDate[tmpShortDate.find("(")+1:tmpShortDate.find(")")]
                tmpShortDate = tmpShortDate.replace(", ", "-")
                
                cur.execute("select dbo.getAmountForCategoryForMonth(?, ?)",category.name,  tmpShortDate )
                row = cur.fetchone()
                amount = row[0]
                if(amount != None):
                    wholeAmountForCategory.append(amount)
                else:
                    wholeAmountForCategory.append(0)
                
                tmp = tmp.addMonths(1)
                tmpShortDate = ""

           
            self.wholeAmount.append(wholeAmountForCategory)    
            self.listOfCategories[category.name] = wholeAmountForCategory

        conn.close()
    
    
    
    def addColumn(self):
        '''
        dodaje tyle kolumn do togetherLayoutu ile jest miesiecy, zapisuje do listy listOfMonthWidget
        '''
        for i in range(len(self.months)):
            amountVerticalLayout = QtGui.QVBoxLayout()
            amountVerticalLayout.setObjectName(_fromUtf8("amountVerticalLayout"))
            monthLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
            monthLabel.setMaximumSize(QtCore.QSize(16777215, 20))
            monthLabel.setObjectName(_fromUtf8("monthLabel"))
            amountVerticalLayout.addWidget(monthLabel)
            self.listOfAmountVerticalLayouts.append(amountVerticalLayout)
            self.togetherHorizontalLayout.addLayout(amountVerticalLayout)
            monthLabel.setText(self.months[i])

    def addCategory(self,category):
        '''
        dodaje przycisk kategorii oraz podsumy do kolejnych kolumn w self.listOfMonthWidget
        '''
        categoryButton = QtGui.QPushButton(self.horizontalLayoutWidget_2)
        categoryButton.setMinimumSize(QtCore.QSize(0, 30))
        categoryButton.setObjectName(_fromUtf8("categoryButton"))
        categoryButton.connect(categoryButton, QtCore.SIGNAL("clicked()"),self.openCategoryWidget)
        self.categoryVerticalLayout.addWidget(categoryButton)
        categoryButton.setText(category)
        for layout in self.listOfAmountVerticalLayouts:
            priceHorizontalLayout = QtGui.QHBoxLayout()
            priceHorizontalLayout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
            priceHorizontalLayout.setObjectName(_fromUtf8("priceHorizontalLayout"))
            priceLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
            priceLabel.setMaximumSize(QtCore.QSize(16777215, 20))
            priceLabel.setObjectName(_fromUtf8("priceLabel"))
            priceHorizontalLayout.addWidget(priceLabel)
            measureLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
            measureLabel.setMaximumSize(QtCore.QSize(16777215, 20))
            measureLabel.setObjectName(_fromUtf8("measureLabel_15"))
            priceHorizontalLayout.addWidget(measureLabel)
            layout.addLayout(priceHorizontalLayout)
            measureLabel.setText(QtGui.QApplication.translate("Form", "zl", None, QtGui.QApplication.UnicodeUTF8))
            priceLabel.setText(QtGui.QApplication.translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n" "p, li { white-space: pre-wrap; }\n" "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"right\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">"+QtCore.QString().setNum(self.listOfCategories[category][self.listOfAmountVerticalLayouts.index(layout, )])+ "</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))

            
    def openCategoryWidget(self):
        self.category = Category(str(self.sender().text()))
        self.categoryWidget = CategoryWidget.Ui_MainWindow()
        self.categoryWidget.setupUi(self.categoryWidget,self.category,self.databaseDir)
        self.categoryWidget.setGeometry(QtCore.QRect(0, 0, 400, 120))
        self.categoryWidget.setAutoFillBackground(True)
        self.categoryWidget.setGeometry(QtCore.QRect(360,180,420,400))
        self.categoryWidget.show()
        
    def genRaport(self):
        self.retranslateUi(self)
        self.getAllCategories()
        self.addColumn()
        for category in self.listOfCategories: self.addCategory(category)
        self.listOfAmountVerticalLayouts = []
        self.listOfCategories.clear()
        self.months = []
        self.wholeAmount = []
        self.categories = []
        
        
        
    def setupUi(self, MonthRaport, date,databaseDir):
        '''
        wygenerowane gui
        '''
        self.databaseDir = databaseDir
        self.date = date
        MonthRaport.resize(431, 491)
        
        #raport label
        self.horizontalLayoutWidget = QtGui.QWidget(MonthRaport)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 401, 41))
        self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget"))
        self.raportHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget)
        self.raportHorizontalLayout.setMargin(0)
        self.raportHorizontalLayout.setObjectName(_fromUtf8("raportHorizontalLayout"))
        self.raportLabel = QtGui.QLabel(self.horizontalLayoutWidget)
        self.raportLabel.setObjectName(_fromUtf8("raportLabel"))
        self.raportHorizontalLayout.addWidget(self.raportLabel)
        self.dataLabel = QtGui.QLabel(self.horizontalLayoutWidget)
        self.dataLabel.setObjectName(_fromUtf8("dataLabel"))
        self.raportHorizontalLayout.addWidget(self.dataLabel)
        
        #data
                #data
        self.startdatafield = QtGui.QDateEdit(MonthRaport)
        self.startdatafield.setGeometry(QtCore.QRect(0, 50, 200, 20))
        self.startdatafield.setMinimumDate(QtCore.QDate.currentDate().addDays(-1000))
        self.startdatafield.setMaximumDate(QtCore.QDate.currentDate().addDays(1000))
        self.startdatafield.setDisplayFormat("yyyy.MM")
        self.startdatafield.show()
       
        self.enddatafield = QtGui.QDateEdit(MonthRaport)
        self.enddatafield.setGeometry(QtCore.QRect(201, 50, 200, 20))
        self.enddatafield.setMinimumDate(QtCore.QDate.currentDate().addDays(-1000))
        self.enddatafield.setMaximumDate(QtCore.QDate.currentDate().addDays(1000))
        self.enddatafield.setDisplayFormat("yyyy.MM")
        self.enddatafield.show()
        
        self.genButton = QtGui.QPushButton(MonthRaport)
        self.genButton.setMinimumSize(QtCore.QSize(0, 30))
        self.genButton.setGeometry(401, 50, 40, 20)
        self.genButton.setObjectName(_fromUtf8("genButton"))
        self.genButton.connect(self.genButton, QtCore.SIGNAL("clicked()"),self.genRaport)
        
        #wykres
        self.chartWidget = QtGui.QWidget(MonthRaport)
        self.chartWidget.setGeometry(QtCore.QRect(20, 290, 381, 171))
        self.chartWidget.setObjectName(_fromUtf8("chartWidget"))
        
        #polaczenie
        self.horizontalLayoutWidget_2 = QtGui.QWidget(MonthRaport)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(19, 99, 381, 182))
        self.horizontalLayoutWidget_2.setObjectName(_fromUtf8("horizontalLayoutWidget_2"))
        self.togetherHorizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.togetherHorizontalLayout.setMargin(0)
        self.togetherHorizontalLayout.setObjectName(_fromUtf8("togetherHorizontalLayout"))
        
        
        #kategorie
        self.categoryVerticalLayout = QtGui.QVBoxLayout()
        self.categoryVerticalLayout.setObjectName(_fromUtf8("categoryVerticalLayout"))
        self.noneLabel = QtGui.QLabel(self.horizontalLayoutWidget_2)
        self.noneLabel.setText(_fromUtf8(" "))
        self.noneLabel.setObjectName(_fromUtf8("noneLabel"))
        self.categoryVerticalLayout.addWidget(self.noneLabel)
        self.togetherHorizontalLayout.addLayout(self.categoryVerticalLayout)
        

        
        


        
        self.retranslateUi(MonthRaport)
        QtCore.QMetaObject.connectSlotsByName(MonthRaport)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.raportLabel.setText(QtGui.QApplication.translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:18pt;\">Raport:</span></p></body></html>", None, QtGui.QApplication.UnicodeUTF8))
